class Admin::CategoriesController < Admin::BasesController
  def index
    @title = t("h2.categories")
    @search = Category.new_search(params[:search])
    unless params[:keyword].blank?
      keyword = split_keyword(params[:keyword])
      @search.conditions.or_name_like = keyword
      @search.conditions.or_description_like = keyword
    end
    @categories = @search.all.paginate(:page => params[:page] || 1, :per_page=> params[:per_page] || 50)
  end

  def new
    @title = t('h2.new_category')
    @category = Category.new
  end

  def create
    @category = Category.new(params[:category])
    if @category.save
      flash[:notice] = "创建成功！"
      redirect_to admin_categories_path
    else
      render :action => :new
    end
  end

  def edit
    @title = t("h2.edit_category")
    @category = Category.find(params[:id])
  end

  def show
    @category = Category.find(params[:id])
    render :edit
  end

  def update
    @category = Category.find(params[:id])
    if @category.update_attributes(params[:category])
      flash[:notice] = "修改成功！"
      redirect_to admin_categories_path
    else
      render :action => 'edit'
    end
  end

  def destroy
    @category = Category.find(params[:id])
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(admin_categories_url) }
      format.xml { head :ok }
    end
  end
end
